﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OreSystem.Entities;
using OreSystem.BLL;
namespace MainFrom {
    public partial class LevelEdit : Form {
        public IList<Level> PLevel { get; set; }
        public int layer { get; set; }
        public Level newLevel { get; set; }
        private LevelBll levelBLL = new LevelBll();
        public LevelEdit() {
            InitializeComponent();
        }

        private void LevelEdit_Load(object sender, EventArgs e) {

            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            cmb_PID.ValueMember = "ID";
            cmb_PID.DisplayMember = "Name";
            cmb_PID.DataSource = PLevel;
            lbl_err.ForeColor = Color.Red;
                       
            if (newLevel !=null && this.layer > 1){
                //cmb_PID.SelectedItem = null;
                //cmb_PID.SelectedItem = levelBLL.GetLevelById(newLevel.PID);
                //cmb_PID.SelectedText = levelBLL.GetLevelById(newLevel.PID).Name;
                cmb_PID.SelectedValue = newLevel.PID;
                txt_LevelNumber.Text = newLevel.LevelNumber;
                txt_Name.Text = newLevel.Name;
            }
            else if (newLevel != null)
            {
                txt_LevelNumber.Text = newLevel.LevelNumber;
                txt_Name.Text = newLevel.Name;
            }
            
        }

        private void btn_save_Click(object sender, EventArgs e) {
            if (cmb_PID.SelectedItem == null) {
                //MessageBox.Show("请添加上册信息！");
                lbl_err.Text = "没有选择上层信息";
                cmb_PID.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txt_LevelNumber.Text)) {
                //MessageBox.Show("层编号不能为空！");
                lbl_err.Text = "层编号不能为空！";
                txt_LevelNumber.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txt_Name.Text)) {
                //MessageBox.Show("负责人姓名不能为空！");
                lbl_err.Text = "负责人姓名不能为空！";
                txt_Name.Focus();
                return;
            }

            try
            {                
                
                if (newLevel == null)
                {
                    levelBLL.SaveLevel(new Level() { Layer = layer, Name = txt_Name.Text, LevelNumber = txt_LevelNumber.Text, Path=levelBLL.GetPathById((int)cmb_PID.SelectedValue), PID = (int)cmb_PID.SelectedValue, CreateDate = DateTime.Now });
                    lbl_err.Text = "保存信息成功！";
                }
                else
                {
                    newLevel.Name = txt_Name.Text;
                    newLevel.LevelNumber = txt_LevelNumber.Text;
                    newLevel.CreateDate = DateTime.Now;
                    newLevel.PID = (int)cmb_PID.SelectedValue;
                    newLevel.Path = levelBLL.GetPathById((int)cmb_PID.SelectedValue);
                    levelBLL.SaveLevel(newLevel);
                    this.DialogResult = DialogResult.OK;
                }
                
                Reset();             
                
            }
            catch (Exception ex)
            {

                MessageBox.Show("出现错误请联系管理员。错误信息"+ex.Message);
            }
            //this.DialogResult = DialogResult.OK;
            //this.Close();
           
        }

        private void btn_clear_Click(object sender, EventArgs e) {
            Reset();
        }
        public void Reset() {
            txt_LevelNumber.Text = "";
            txt_Name.Text = "";
            cmb_PID.Focus();
        }

        private void cmb_PID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                txt_LevelNumber.Focus();
            }
        }

        private void txt_Name_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btn_save.Focus();
            }
        }

        private void txt_LevelNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                txt_Name.Focus();
            }
        }
    }
}
